/*
 * Copyright (C) 2020 Honeywell, Inc. All Rights Reserved.
 */
package org.thanos.iot.dao.graphic;

import com.honeywell.tj.iot.entity.graphic.GraphicItem;
import com.honeywell.tj.iot.mapper.graphic.dao.GraphicItemProvider;
import com.honeywell.tj.iot.vo.graphic.item.Item;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @ClassName ItemMapper
 * @Author H241118
 * @Date 3/10/2020 9:23 AM
 **/
@Mapper
public interface GraphicItemMapper {

	/**
	 * 绝对不能删
	 * @param viewId
	 * @return
	 */
	@Results({ @Result(id = true, column = "id", property = "id", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_top", property = "layoutTop", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_left", property = "layoutLeft", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_width", property = "layoutWidth", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_height", property = "layoutHeight", jdbcType = JdbcType.INTEGER),
			@Result(column = "component_id", property = "componentId", jdbcType = JdbcType.INTEGER),
			@Result(column = "view_id", property = "viewId", jdbcType = JdbcType.INTEGER),
			@Result(column="uuid",property="uuid", jdbcType = JdbcType.VARCHAR),
			@Result(column="editable",property="editable", jdbcType = JdbcType.INTEGER)
	})
	@Select("SELECT * FROM iot_graphic_item WHERE view_id = #{viewId} ORDER BY id")
    List<GraphicItem> selectItemsByViewId(Long viewId);

	@Select("SELECT uuid FROM iot_graphic_item WHERE view_id = #{viewId} ORDER BY id")
    List<String> selectUuidsByViewId(Integer viewId);

	@Results({
			@Result(column = "component_id", property = "componentId", jdbcType = JdbcType.INTEGER)
	})
	@Select("SELECT component_id FROM iot_graphic_item WHERE uuid = #{uuid} LIMIT 1")
    Integer selectComponentIdByUuid(String uuid);

	@Results({@Result(id = true, column = "id", property = "id", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_top", property = "layoutTop", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_left", property = "layoutLeft", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_width", property = "layoutWidth", jdbcType = JdbcType.INTEGER),
			@Result(column = "layout_height", property = "layoutHeight", jdbcType = JdbcType.INTEGER),
			@Result(column = "component_id", property = "componentId", jdbcType = JdbcType.INTEGER),
			@Result(column = "view_id", property = "viewId", jdbcType = JdbcType.INTEGER),
			@Result(column = "uuid", property = "uuid", jdbcType = JdbcType.VARCHAR),
			@Result(column = "editable", property = "editable", jdbcType = JdbcType.INTEGER),
			@Result(column = "text", property = "text", jdbcType = JdbcType.VARCHAR)
	})
	@Select("SELECT id, layout_top, layout_left, layout_width, layout_height, component_id, view_id, uuid, editable, text FROM iot_graphic_item WHERE uuid = #{uuid} LIMIT 1")
	GraphicItem selectItemByUuid(String uuid);

	@InsertProvider(type = GraphicItemProvider.class, method = "dynamicAddItems")
    Integer insertItem(Item item);

	@Delete("DELETE FROM iot_graphic_item WHERE view_id = #{viewId}")
	int deleteItemByViewId(Integer viewId);
}
